"ui";
importClass(org.opencv.imgcodecs.Imgcodecs);//导入opencv的包
const tool = require('./tool.js');
const car = require('./car.js');
const dyfd = require('./dyfd.js');
const wxfd = require('./wxfd.js');

var wxfdtypes = "综合|全部|服饰|鞋靴箱包|家纺布艺|食品|酒水|生鲜|美妆|珠宝文玩|图书|数码家电|家清日用|家装建材|母婴|玩具乐器|运动户外|汽摩|宠物绿植|本地生活|教育培训"
ui.layout(
    <vertical h="4000">
        <horizontal>
            <text text="搜索词" />
            <input id="searchWord" w="200" text="食品" />
        </horizontal>
        <horizontal>
            <text text="评论词" />
            <input id="pinglun" w="400" text="求中福袋，求中福袋，点我主页有惊喜" />
        </horizontal>

        <horizontal>
            <text text="可以参与的福袋最长等待时间" />
            <input id="maxOpenTime" inputType="number" w="40" text="180" />
        </horizontal>
        <horizontal>
            <text text="立即进入开奖的最短时间(秒)" />
            <input id="minOpenTime" inputType="number" w="40" text="120" />
        </horizontal>
        <horizontal>
            <text text="可以参与的福袋最低价值" />
            <input id="maxMoney" inputType="number" w="40" text="100" />
        </horizontal>
        <horizontal>
            <text text="最多参与人数" />
            <input id="maxPeopleCount" inputType='number' w="80" text="1000" />
        </horizontal>
        <horizontal>
            <text text="黑名单直播间" />
            <input id="blackLiveList" inputType='textMultiLine' w="200" h="auto" text="与辉同行,游鹿鹿很认真,东方甄选,辰辰妈,浩哥女装,东方甄选美丽生活,云舍花卉,顽皮小老板手作碱水贝果,湖远行" />
        </horizontal>

        <horizontal>
            <text text="中奖提示声音次数" />
            <input id="winCount" inputType="number" w="40" text="5" />
            <Switch id="isenterTotalLive" text="总榜直播间" gravity="center" w="150" h="20" />
        </horizontal>
        <horizontal>
            <text text="微信福袋类型" />
            <spinner id="wxfdtype" entries="{{wxfdtypes}}" />
            <Switch id="isOpenConsole" text="控制台" gravity="center" w="100" h="20" />
        </horizontal>
        <horizontal>
            <text text="卡密" />
            <input id="keygen" w="200" text="" />
            <button id="saveParam" text="保存" />
        </horizontal>
        <horizontal>
            <Switch id="isOpenpinglun" text="评论开关" gravity="center" w="150" h="20" />
        </horizontal>
        <horizontal>
            <button id="dyfd" text="抖音福袋" />
            <button id="wxfd" text="微信福袋" />
        </horizontal>
    </vertical>
);



//抢抖音福袋
ui.dyfd.click(function () {
    threads.start(function () {
        if (!car.checkCar()) {
            return
        }
        let params = collectParameters()
        tool.init(params)
        dyfd.main(params)
    });
});

//抢微信福袋
ui.wxfd.click(function () {
    threads.start(function () {
        if (!car.checkCar()) {
            return
        }
        let params = collectParameters()
        tool.init(params)
        wxfd.main(params)
    });
});

//保存
ui.saveParam.click(function () {
    //保存参数
    let params = collectParameters()
    //效验卡密
    car.updateCardInfo(params)
})

//收集界面配置参数
function collectParameters() {
    var params = {}
    //搜索词
    params.searchWord = ui.searchWord.text()
    //评论
    params.pinglun = ui.pinglun.text()
    //可以参与的福袋最长等待时间
    params.maxOpenTime = ui.maxOpenTime.text()
    //立即进入开奖的最短时间(秒)
    params.minOpenTime = ui.minOpenTime.text()
    //可以参与的福袋最低价值
    params.maxMoney = ui.maxMoney.text()
    //最多参与人数
    params.maxPeopleCount = ui.maxPeopleCount.text()
    //黑名单直播间
    params.blackLiveList = ui.blackLiveList.text().split(',')
    //中奖提示声音次数
    params.winCount = ui.winCount.text()
    //总榜直播间
    params.isenterTotalLive = ui.isenterTotalLive.checked
    //微信福袋类型
    params.wxfdtype = ui.wxfdtype.getSelectedItem();
    //控制台开关
    params.isOpenConsole = ui.isOpenConsole.checked;
    //评论开关
    params.isOpenpinglun = ui.isOpenpinglun.checked
    //卡密
    params.keygen = ui.keygen.text()

    return params
}

//初始化配置数据
function initParameters() {
    try {
        //读取配置文件
        let params = car.readConfig()

        //界面配置参数
        if (params.searchWord !== "" && params.searchWord !== undefined && params.searchWord !== null) {
            ui.searchWord.text(params.searchWord)
        }
        if (params.pinglun !== "" && params.pinglun !== undefined && params.pinglun !== null) {
            ui.pinglun.text(params.pinglun)
        }
        if (params.maxOpenTime !== "" && params.maxOpenTime !== undefined && params.maxOpenTime !== null) {
            ui.maxOpenTime.text(params.maxOpenTime)
        }
        if (params.minOpenTime !== "" && params.minOpenTime !== undefined && params.minOpenTime !== null) {
            ui.minOpenTime.text(params.minOpenTime)
        }
        if (params.maxMoney !== "" && params.maxMoney !== undefined && params.maxMoney !== null) {
            ui.maxMoney.text(params.maxMoney)
        }

        if (params.maxPeopleCount !== "" && params.maxPeopleCount !== undefined && params.maxPeopleCount !== null) {
            ui.maxPeopleCount.text(params.maxPeopleCount)
        }


        if (params.blackLiveList !== "" && params.blackLiveList !== undefined && params.blackLiveList !== null) {
            ui.blackLiveList.text(params.blackLiveList.join(','))
        }
        if (params.winCount !== "" && params.winCount !== undefined && params.winCount !== null) {
            ui.winCount.text(params.winCount)
        }

        //总榜直播间
        if (params.isenterTotalLive !== "" && params.isenterTotalLive !== undefined && params.isenterTotalLive !== null) {
            ui.isenterTotalLive.setChecked(params.isenterTotalLive)
        }

        if (params.wxfdtype !== "" && params.wxfdtype !== undefined && params.wxfdtype !== null) {
            let index = wxfdtypes.split('|').indexOf(params.wxfdtype)
            ui.wxfdtype.setSelection(index, true);
        }
        if (params.isOpenConsole !== "" && params.isOpenConsole !== undefined && params.isOpenConsole !== null) {
            ui.isOpenConsole.setChecked(params.isOpenConsole)
        }
        //评论开关
        if (params.isOpenpinglun !== "" && params.isOpenpinglun !== undefined && params.isOpenpinglun !== null) {
            ui.isOpenpinglun.setChecked(params.isOpenpinglun)
        }
        //卡密
        if (params.keygen !== "" && params.keygen !== undefined && params.keygen !== null) {
            ui.keygen.text(params.keygen)
        }
        return params
    } catch (e) {
        console.log("初始化数据异常:" + e)
    }
}




//初始化配置数据
initParameters()
